import axios from 'axios'
import { Message } from 'element-ui'
import { getToken } from '@/utils/auth'
import Config from '@/settings'
import store from '@/store'
import router from '@/router/routers'
// 进度条
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

// 创建axios实例
const service = axios.create({
  baseURL: process.env.VUE_APP_BASE_API,

  timeout: Config.timeout // 请求超时时间
})

// request拦截器
service.interceptors.request.use(
  config => {
    // 开启进度条
    NProgress.start()
    if (getToken()) {
      // 让每个请求携带自定义token
      config.headers['X-Token'] = getToken()
    }
    return config
  },
  error => {
    // do something with request error
    console.log(error)
    return Promise.reject(error)
  }
)

// response 拦截器
service.interceptors.response.use(
  response => {
    // 关闭进度条
    NProgress.done()

    // 处理文件流
    const resType = response.request.responseType
    // 处理文件流
    if (resType === 'blob') {
      return response.data
    }
    const res = response.data

    // 处理错误信息
    if (res.code !== 20000) {
      Message({
        message: res.message || 'Error',
        type: 'error',
        duration: 5 * 1000
      })

      if (res.code === 90001) { // token过期，返回到登陆页面
        // 延迟加载是为了提示更明显
        setTimeout(function() {
          store.dispatch('Logout').then(() => {
            location.reload()
          })
        }, 1000)
      }

      // 没有权限跳转403页面
      if (res.code === 90003) {
        router.push({ path: '/401' })
      }

      return Message
    } else {
      return res
    }
  },
  error => {
    console.log('err' + error) // for debug
    Message({
      message: error.message,
      type: 'error',
      duration: 5 * 1000
    })
    return Promise.reject(error)
  }
)

export default service
